<script src="#(CPATH)/static/components/wangEditor/index.js"></script>
<script>
    const { createEditor, createToolbar } = window.wangEditor

    const editorConfig = {
        placeholder: 'Type here...',
        onChange(editor) {
            const html = editor.getHtml()
            console.log('editor content', html)
            // 也可以同步到 <textarea>
        }
    }

    window.editor = createEditor({
        selector: '#editor-container',
        html: '',
        config: editorConfig,
        mode: 'default', // or 'simple'
    })

    const toolbarConfig = {}

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'default', // or 'simple'
    })



    window.insertImageToEditor = function (src,title){
        var img = document.createElement('img') // 添加的内容可以自定义， 这里以添加图片为例
        img.setAttribute('src', src)
        img.setAttribute('alt', title)
        insertHtmlToEditor(img.outerHTML);
    }


    window.insertVideoToEditor = function (src,title){
        var video = document.createElement('video') // 添加的内容可以自定义， 这里以添加图片为例
        video.setAttribute('src', src)
        video.setAttribute('style', 'max-width:100%;')
        video.setAttribute('controls', 'controls')

        insertHtmlToEditor(video.outerHTML);
    }


    window.insertAttachementToEditor = function (src,title){
        var a = document.createElement('a') // 添加的内容可以自定义， 这里以添加图片为例
        a.setAttribute('href', src)
        a.setAttribute("target", "_blank")
        a.innerHTML = title;
        insertHtmlToEditor('<p class="attachment-item">附件：' + a.outerHTML+"</p>");
    }


    window.insertHtmlToEditor = function (html){
        window.editor.insertText(" ");
        window.editor.dangerouslyInsertHtml(html);
    }


    window.onEditorSubmitBefore = function (){
        $("#content").val(window.editor.getHtml());
    }

</script>